{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出实现$f(x) = sin^2(x-2){e^{-x^2}}$的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: -0.911685\n",
      "         Iterations: 20\n",
      "         Function evaluations: 40\n",
      "[0.21625]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy import optimize\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "def func(x):\n",
    "    return -np.sin(x - 2) ** 2 * np.exp(-x * x)\n",
    "\n",
    "maximum = optimize.fmin(func, 0)\n",
    "print(maximum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "建立矩阵$A∈R^{m*n},m > n$,同时建立向量$b∈ R^m$,输出$x = \\arg \\min_x||Ax - b||_2$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A =\n",
      " [[0.29951483 0.85837021 0.063572   0.86240935]\n",
      " [0.1659896  0.66006526 0.71296158 0.18514093]\n",
      " [0.65799356 0.92943923 0.56945286 0.36160131]\n",
      " [0.61902533 0.26214423 0.66423021 0.52208459]\n",
      " [0.36951682 0.2762627  0.17270742 0.52593687]]\n",
      "b =\n",
      " [[0.44061639]\n",
      " [0.45112777]\n",
      " [0.7718685 ]\n",
      " [0.17556259]\n",
      " [0.02821315]]\n",
      "Solution =\n",
      " [[ 0.30962894]\n",
      " [ 0.81684096]\n",
      " [-0.05753041]\n",
      " [-0.43624169]]\n",
      "Norm = [0.00188785]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy import linalg\n",
    "\n",
    "m, n = 5, 4\n",
    "A = np.mat(np.random.rand(m, n))\n",
    "b = np.mat(np.random.rand(m, 1))\n",
    "\n",
    "x, res, rnk, s = linalg.lstsq(A, b)\n",
    "\n",
    "norm = linalg.norm(A.dot(x) - b, ord = 2)\n",
    "\n",
    "print(\"A =\\n\",A)\n",
    "print(\"b =\\n\",b)\n",
    "print(\"Solution =\\n\", x)\n",
    "print(\"Norm =\", res / n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对一个包含n行m列的矩阵X，如何计算每两行之间的成对距离？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X =\n",
      " [[0.49056706 0.79617414]\n",
      " [0.40252888 0.79432963]\n",
      " [0.76432606 0.91551883]\n",
      " [0.37802745 0.17255606]\n",
      " [0.5936573  0.8283253 ]]\n",
      "Distance =\n",
      " [[0.         0.0880575  0.29864217 0.6336913  0.10798747]\n",
      " [0.0880575  0.         0.38155475 0.62225612 0.19412825]\n",
      " [0.29864217 0.38155475 0.         0.83738897 0.19165213]\n",
      " [0.6336913  0.62225612 0.83738897 0.         0.69031118]\n",
      " [0.10798747 0.19412825 0.19165213 0.69031118 0.        ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.spatial import distance\n",
    "\n",
    "m, n = 5, 2\n",
    "x = np.random.rand(m, n)\n",
    "print(\"X =\\n\", x)\n",
    "y = distance.pdist(x)\n",
    "z = distance.squareform(y)\n",
    "print(\"Distance =\\n\", z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
